#include <stdio.h>
#include "course.h"

void ExpectedFinalGrade (struct Course *courses)
{
	int choice, i, j, k, selection;
	float expectedTotal, neededFinal;
	int courseCount = getCourseNum(courses); 

	printf("\n============== Calculate Expected Final ==============\n");
	printf(" 1. View All Courses\n");
	printf(" 2. View Single Course\n");
	printf(" 3. Back\n");
	printf("======================================================\n\n");
	printf("Enter a number to choose an operation: ");
	scanf("%d", &choice);

	if (choice == 1) {
		while(1){
			printf("Enter an expected course total([0, 100]) or any negative to go back: ");
			scanf("%f", &expectedTotal);

			if (expectedTotal < 0 || expectedTotal > 100) {
            			return; // Go back if negative number is entered
			}

			printf("\n | Danger   | Course Code    | Course Name                   | Lowest Final for Expected Course Total |\n");
        
			for (i = 0; i < courseCount; i++) {
				if (courses[i].code[0] == '\0') break; // Assuming empty course code indicates the end of course list

			// Calculate the needed score for the final exam
				float currentTotal = 0;
				int gradeItems = getGradeItemNum(courses[i]);

				for (j = 0; j < gradeItems - 1; j++){
					int gradeNum = getGradeNum(courses[i].gradeitems[j]);
					float totalGrades = 0;
					for (k = 0; k < gradeNum; k++){
						totalGrades += courses[i].gradeitems[j].grades[k];
					}
					currentTotal += totalGrades / gradeNum / courses[i].gradeitems[j].fullmark * courses[i].gradeitems[j].percentage;
				}
				neededFinal = (expectedTotal - currentTotal) * 100 / courses[i].gradeitems[gradeItems-1].percentage;
			
			// Check if the needed score is achievable
				if (neededFinal <= 100 && neededFinal >= 0) {
					printf(" |          | %-15s| %-30s| %-39.2f|\n", courses[i].code, courses[i].name, neededFinal);
				} 
				else {
					printf(" | *        | %-15s| %-30s| %-39.2f|\n", courses[i].code, courses[i].name, neededFinal);
				}
				if (i == courseCount-1) {
					printf("\n");
				}
			}
		}
	}




	if (choice == 2) {
		printf("There're totally %d courses recorded:\n", courseCount);
		for (int j = 0; j < courseCount; j++){
			printf("\n%d.\t%s\t%s\n", j + 1, courses[j].code, courses[j].name);
		}
		printf("\nEnter a number to select one course or any other key back: ");
		scanf("%d", &selection); // course selection
		if (selection > courseCount) {
			return;
		}
		while(1){
			printf("Enter an expected course total([0, 100]) or any negative to go back: ");
			scanf("%f", &expectedTotal);

			if (expectedTotal < 0 || expectedTotal > 100) {
				return; // Go back if negative number is entered
			}

			// Calculate the needed score for the final exam
			i = selection - 1;
			float currentTotal = 0;
			printf("\n\t%s %s\n", courses[i].code, courses[i].name);
			int gradeItems = getGradeItemNum(courses[i]);

			for (j = 0; j < gradeItems - 1; j++){
				printf("\t-----------------------------------\n");
				printf("\t| No.    | Item                 | Percentage | Original Average     | Item Grade      |\n");
				printf("\t| %d      | %-21s| %-11.2f| ", j + 1, courses[i].gradeitems[j].name, courses[i].gradeitems[j].percentage);
				int gradeNum = getGradeNum(courses[i].gradeitems[j]);
				float totalGrades = 0;
				for (k = 0; k < gradeNum; k++){
					totalGrades += courses[i].gradeitems[j].grades[k];
				}
				printf("%-21.2f| ", totalGrades / gradeNum);
				printf("%-16.2f|\n", totalGrades / gradeNum / courses[i].gradeitems[j].fullmark * courses[i].gradeitems[j].percentage);
				printf("\t-----------------------------------\n\n");
				currentTotal += totalGrades / gradeNum / courses[i].gradeitems[j].fullmark * courses[i].gradeitems[j].percentage;
			}

			printf("\t-----------------------------------\n");
			printf("\t| No.    | Item                 | Percentage | Fullmark             | Expected Grade  |\n");
			neededFinal = (expectedTotal - currentTotal) * 100 / courses[i].gradeitems[gradeItems-1].percentage;
			
			// Check if the needed score is achievable
			if (neededFinal <= 100 && neededFinal >= 0) {
				printf("\t| %d      | %-21s| %-11.2f| %-21.2f| %-16.2f|\n", gradeItems, courses[i].gradeitems[gradeItems-1].name, courses[i].gradeitems[j].percentage, courses[i].gradeitems[gradeItems-1].fullmark, neededFinal);
			} 
			else {
				printf("\t| *%d     | %-21s| %-11.2f| %-21.2f| %-16.2f|\n", gradeItems, courses[i].gradeitems[gradeItems-1].name, courses[i].gradeitems[j].percentage, courses[i].gradeitems[gradeItems-1].fullmark, neededFinal);
			}
			printf("\t-----------------------------------\n");
			printf("ATTENTION: The final score should pass a certain threshold to pass the course!\n\n");
		}
	}




	else if (choice == 3) {
		return; // Go back to main menu
	}
}

